package com.woyou.model;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.EBean.Scope;

import retrofit.RetrofitError;
import android.text.TextUtils;
import android.util.Log;

import com.citaq.ideliver.bean.User;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.woyou.Constant;
import com.woyou.bean.City;
import com.woyou.bean.CodeListResult;
import com.woyou.bean.CodeResult;
import com.woyou.bean.MyAddress;
import com.woyou.bean.MyOrderItem;
import com.woyou.bean.Result;
import com.woyou.bean.ShopItem;
import com.woyou.bean.UserInfo;
import com.woyou.bean.rpc.CheckUpdateReq;
import com.woyou.bean.rpc.CheckUpdateRes;
import com.woyou.bean.rpc.FeedBackReq;
import com.woyou.bean.rpc.FeedBackRes;
import com.woyou.bean.rpc.LoginReq;
import com.woyou.bean.rpc.MD5Req;
import com.woyou.bean.rpc.ManagerAddrReq;
import com.woyou.bean.rpc.MyOrderReq;
import com.woyou.bean.rpc.MyShopListReq;
import com.woyou.bean.rpc.PhonePwdReq;
import com.woyou.bean.rpc.PhoneReq;
import com.woyou.bean.rpc.PwdCheckReq;
import com.woyou.bean.rpc.UpdateUserInfoReq;
import com.woyou.bean.rpc.UploadPicReq;
import com.woyou.model.rpc.UserService;
import com.woyou.utils.CompatiblePreferncesUtil;
import com.woyou.utils.InputStreamUtils;
import com.woyou.utils.SharedpreferncesUtil;

/**
 * 用户个人信息控制器
 * 
 * @author tanglc
 * 
 */
@SuppressWarnings({ "rawtypes", "unused", "unchecked" })
@EBean(scope = Scope.Singleton)
public class UserModel extends SuperModel {

	private UserService userService;

	public static final String USER = "user";// 1.9 用户信息在缓存中的key

	private static final String ADDRESS_KEYS = "AddressKeys";

	private static final String TAG = "UserController";

	@AfterInject
	// 在构造函数调用之后执行
	@Override
	protected void onInit() {
		userService = mNeWrapper.getNetService(UserService.class);
	}

	/**
	 * 用户登录接口调用
	 * 
	 * @param LoginReq
	 * @return UserInfo
	 */
	public CodeResult<UserInfo> userLogin(LoginReq loginreq)
			throws RetrofitError {
		MD5Req<LoginReq> md5Req = new MD5Req<LoginReq>(loginreq);
		CodeResult<UserInfo> result = userService.userLogin(md5Req.jsonParams,
				md5Req.isEncrypted, md5Req.timeStamp, md5Req.randomNum,
				md5Req.sign);
		// CodeResult<UserInfo> result = queryUserInfoTest();
		if (result.code == 0 || result.code == 1) { // 返回结果（0：用户第一次登录(注册)；1：登录成功；-1：验证码错误；-2：未知错误）
			UserInfo userInfo = new UserInfo();
			userInfo = result.getBean();
			setUserId(userInfo.getuId());
			setUserPhone(loginreq.getPhone());
			setUserPwd(loginreq.getPwd());
			setUservCode(loginreq.getvCode());
			setUserToken(loginreq.getToken());
		}
		return result;
	}

	/**
	 * 更新用户信息接口调用
	 * 
	 * @param updateUserInfoReq
	 * @return CodeResult(code, msg)
	 */
	public CodeResult updateUserInfo(UpdateUserInfoReq updateUserInfoReq)
			throws RetrofitError {
		MD5Req<UpdateUserInfoReq> md5Req = new MD5Req<UpdateUserInfoReq>(
				updateUserInfoReq);
		CodeResult result = userService.updateUserInfo(md5Req.jsonParams,
				md5Req.isEncrypted, md5Req.timeStamp, md5Req.randomNum,
				md5Req.sign);
		// CodeResult result = mockUpdateUserInfo();
		if (result.getCode() == 1) {
			setUserName(updateUserInfoReq.getName());
			setUserSex(updateUserInfoReq.getSex());
			setUserBirth(updateUserInfoReq.getBirth());
			setUserPhone(updateUserInfoReq.getPhone());
		}
		return result;
	}

	/**
	 * 获取用户信息
	 * 
	 * @param uId
	 * @return UserInfo
	 */
	// public CodeResult<UserInfo> queryUserInfo(PhonePwdReq phonePwdReq)
	// throws RetrofitError {
	// MD5Req<PhonePwdReq> md5Req = new MD5Req<PhonePwdReq>(phonePwdReq);
	// CodeResult<UserInfo> result = userService.queryUserInfo(
	// md5Req.jsonParams, md5Req.isEncrypted, md5Req.timeStamp,
	// md5Req.randomNum, md5Req.sign);
	// // CodeResult<UserInfo> result = queryUserInfoTest();
	// UserInfo userInfo = new UserInfo();
	// if (result.code == 1) {
	// userInfo = result.getBean();
	// cacheUserInfo(userInfo);
	// }
	// return result;
	// }

	/**
	 * 管理我的地址
	 * 
	 * @param managerAddr
	 * @return managerAddr
	 */
	public CodeListResult<MyAddress> managerAddr(ManagerAddrReq managerAddrReq)
			throws RetrofitError {
		MD5Req<ManagerAddrReq> md5Req = new MD5Req<ManagerAddrReq>(
				managerAddrReq);
		CodeListResult<MyAddress> result = userService.managerAddr(
				md5Req.jsonParams, md5Req.isEncrypted, md5Req.timeStamp,
				md5Req.randomNum, md5Req.sign);
		// CodeListResult<MyAddress> result = mockManagerAddr();
		if (result.code == 1) {
			List<MyAddress> addrList = result.getList();
			if (addrList != null && addrList.size() > 0) {
				saveAddrList(addrList);
			} else {
				addressDao.clearList();
			}
		}
		return result;
	}

	/**
	 * 上传用户头像接口
	 * 
	 * @param uId
	 * @return UserInfo
	 */
	public Result<UserInfo> v2_1uploadUserPic(UploadPicReq uploadPicReq,
			String path) throws Exception {
		MD5Req<UploadPicReq> md5Req = new MD5Req<UploadPicReq>(uploadPicReq);

		String req = Constant.SERVER
				+ "v2_1uploadPic?isEncrypted=%s&timeStamp=%s&randomNum=%s&sign=%s&params=%s";
		String sUrl = String.format(req, md5Req.isEncrypted, md5Req.timeStamp,
				md5Req.randomNum, md5Req.sign,
				URLEncoder.encode(md5Req.jsonParams, "UTF-8"));
		String end = "\r\n";
		String twoHyphens = "--";
		String boundary = "*****";

		File file = new File(path);

		URL url = new URL(sUrl);
		HttpURLConnection con = (HttpURLConnection) url.openConnection();
		/* 允许Input、Output，不使用Cache */
		con.setDoInput(true);
		con.setDoOutput(true);
		con.setUseCaches(false);
		con.setConnectTimeout(15000);
		/* 设置传送的method=POST */
		con.setRequestMethod("POST");
		/* setRequestProperty */
		con.setRequestProperty("Connection", "Keep-Alive");
		con.setRequestProperty("Charset", "UTF-8");
		con.setRequestProperty("Content-Type", "multipart/form-data;boundary="
				+ boundary);
		/* 设置DataOutputStream */
		DataOutputStream ds = new DataOutputStream(con.getOutputStream());
		ds.writeBytes(twoHyphens + boundary + end);
		ds.writeBytes("Content-Disposition: form-data; "
				+ "name=\"file1\";filename=\"MyFile.png\"" + end);
		ds.writeBytes(end);
		/* 取得文件的FileInputStream */
		FileInputStream fStream = new FileInputStream(file);
		/* 设置每次写入1024bytes */
		int bufferSize = 1024;
		byte[] buffer = new byte[bufferSize];
		int length = -1;
		/* 从文件读取数据至缓冲区 */
		while ((length = fStream.read(buffer)) != -1) {
			/* 将资料写入DataOutputStream中 */
			ds.write(buffer, 0, length);
		}
		ds.writeBytes(end);
		ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
		/* close streams */
		fStream.close();
		ds.flush();
		/* 取得Response内容 */
		InputStream is = con.getInputStream();
		// int ch;
		// StringBuffer b = new StringBuffer();
		// while ((ch = is.read()) != -1) {
		// b.append((char) ch);
		// }
		String b = InputStreamUtils.InputStreamTOString(is);
		/* 关闭DataOutputStream */
		ds.close();
		String res = b;
		Type type = new TypeToken<Result<UserInfo>>() {
		}.getType();
		Result<UserInfo> result = new Gson().fromJson(res, type);
		if (result.code == 1) {
			UserInfo userInfo = result.getData();
			setUserPicUrl(userInfo.getPicUrl());
		}
		return result;

	}

	/**
	 * 获取验证码接口
	 * 
	 * @param PhoneReq
	 * @return CodeResult
	 */
	public CodeResult getCheckCode(PhoneReq phoneReq) throws RetrofitError {
		MD5Req<PhoneReq> md5Req = new MD5Req<PhoneReq>(phoneReq);
		CodeResult result = userService.getCheckCode(md5Req.jsonParams,
				md5Req.isEncrypted, md5Req.timeStamp, md5Req.randomNum,
				md5Req.sign);
		// CodeResult result = mockUpdateUserInfo();
		return result;
	}

	/**
	 * 我的收藏店铺列表
	 * 
	 * @param PhoneReq
	 * @return CodeResult
	 */
	public Result<List<ShopItem>> v2_3myShopList(MyShopListReq myShopListReq)
			throws RetrofitError {
		MD5Req<MyShopListReq> md5Req = new MD5Req<MyShopListReq>(myShopListReq);
		Result<List<ShopItem>> result = userService.v2_3myShopList(
				md5Req.jsonParams, md5Req.isEncrypted, md5Req.timeStamp,
				md5Req.randomNum, md5Req.sign);
		// CodeListResult<ShopItem> result = mockMyShopList();
		return result;
	}

	/**
	 * 2.9 查询订单列表（http://xxxx/ v2_1queryMyOrder）
	 * 
	 * @param myOrderReq
	 * @return
	 * @throws RetrofitError
	 */
	public Result<List<MyOrderItem>> v2_3queryMyOrder(MyOrderReq myOrderReq)
			throws RetrofitError {
		MD5Req<MyOrderReq> md5Req = new MD5Req<MyOrderReq>(myOrderReq);
		Result<List<MyOrderItem>> result = userService.v2_3queryMyOrder(
				md5Req.jsonParams, md5Req.isEncrypted, md5Req.timeStamp,
				md5Req.randomNum, md5Req.sign);
//		Result<List<MyOrderItem>> result = mockQueryMyOrder();
		return result;
	}

	/**
	 * 意见反馈（http://xxxx/ feedback）
	 * 
	 * @param FeedBackReq
	 * @return;
	 * @throws RetrofitError
	 */
	// public CodeResult<FeedBackRes> feedback(FeedBackReq feedBackReq)
	// throws RetrofitError {
	// MD5Req<FeedBackReq> md5Req = new MD5Req<FeedBackReq>(feedBackReq);
	// CodeResult<FeedBackRes> result = userService.feedback(md5Req.jsonParams,
	// md5Req.isEncrypted, md5Req.timeStamp,md5Req.randomNum, md5Req.sign);
	// return result;
	// }

	/**
	 * 2.28 手机信息上传和检查版本更新 （http://xxxx/checkUpdate）
	 * 
	 * @param CheckUpdateReq
	 * @return CheckUpdateRes
	 * @throws RetrofitError
	 */
	public CodeResult<CheckUpdateRes> checkUpdate(CheckUpdateReq checkUpdateReq)
			throws RetrofitError {
		MD5Req<CheckUpdateReq> md5Req = new MD5Req<CheckUpdateReq>(
				checkUpdateReq);
		CodeResult<CheckUpdateRes> result = userService.checkUpdate(
				md5Req.jsonParams, md5Req.isEncrypted, md5Req.timeStamp,
				md5Req.randomNum, md5Req.sign);
		// CodeResult<CheckUpdateRes> result = mockCheckUpdateRes();
		return result;
	}

	/**
	 * 2.1版本接口分割线---------------------------------------------------------------
	 * ----------
	 */

	/**
	 * 意见反馈2.1（http://xxxx/ v2_1feedback）
	 * 
	 * @param FeedBackReq
	 * @return
	 * @throws RetrofitError
	 */
	public Result<FeedBackRes> v2_1feedback(FeedBackReq feedBackReq)
			throws RetrofitError {
		MD5Req<FeedBackReq> md5Req = new MD5Req<FeedBackReq>(feedBackReq);
		Result<FeedBackRes> result = userService.v2_1feedback(
				md5Req.jsonParams, md5Req.isEncrypted, md5Req.timeStamp,
				md5Req.randomNum, md5Req.sign);
		return result;
	}

	/**
	 * 获取用户信息2.1
	 * 
	 * @param uId
	 * @return UserInfo
	 */
	public Result<UserInfo> v2_1queryUserInfo(PhonePwdReq phonePwdReq)
			throws RetrofitError {
		MD5Req<PhonePwdReq> md5Req = new MD5Req<PhonePwdReq>(phonePwdReq);
		Result<UserInfo> result = userService.v2_1queryUserInfo(
				md5Req.jsonParams, md5Req.isEncrypted, md5Req.timeStamp,
				md5Req.randomNum, md5Req.sign);
		// Result<UserInfo> result = queryUserInfoTest();
		UserInfo userInfo = new UserInfo();
		if (result.code == 1) {
			userInfo = result.getData();
			cacheUserInfo(userInfo);
		}
		return result;
	}

	/**
	 * 获取本地缓存的所有用户信息
	 * 
	 * @return info
	 */
	public UserInfo getUserInfo() {
		UserInfo info = new UserInfo();
		info.setuId(mUserDao.id().get());
		info.setName(mUserDao.name().get());
		info.setNick(mUserDao.nick().get());
		info.setBirth(mUserDao.birth().get());
		info.setPhone(mUserDao.phone().get());
		info.setPwd(mUserDao.pwd().get());
		info.setvCode(mUserDao.vCode().get());
		info.setToken(mUserDao.token().get());
		info.setPicUrl(mUserDao.picUrl().get());
		City city = new City();
		city.setCity(mUserDao.city().get());
		city.setCityCode(mUserDao.cityCode().get());
		info.setCity(city);
		info.setCoupon(mUserDao.coupon().get());
		info.setIntegral(mUserDao.integral().get());
		info.setWallet(mUserDao.wallet().get());
		info.setSex(mUserDao.sex().get());

		return info;
	}

	public int getUpdateNum() {
		int num = mUserDao.updateNum().get();
		return num;
	}

	/**
	 * 兼容1.9
	 * 
	 * @return
	 */
	public UserInfo getCompatibleUserInfo() {
		UserInfo info = new UserInfo();
		String uId = mUserDao.id().get();
		if (TextUtils.isEmpty(uId)) {
			User oldUser = getOldUserInfo();
			if (oldUser != null) {
				Log.i(TAG, "存在1.9遗留用户信息数据");
				// 如果存在1.9的用户信息
				setUserName(oldUser.UserName);
				setUserNick(oldUser.NickName);
				setUserPhone(oldUser.Phone);
				setUserId(oldUser.UserId);
				setUserPwd(oldUser.PWD);
				info.setuId(oldUser.UserId);
				info.setPwd(oldUser.PWD);
				clearOldUserInfo();
			} else {
				Log.i(TAG, "无1.9遗留数据");
				getUserInfoByDao(info);
			}

		} else {
			getUserInfoByDao(info);
		}

		return info;
	}

	private void getUserInfoByDao(UserInfo info) {
		info.setuId(mUserDao.id().get());
		info.setName(mUserDao.name().get());
		info.setNick(mUserDao.nick().get());
		info.setBirth(mUserDao.birth().get());
		info.setPhone(mUserDao.phone().get());
		info.setPwd(mUserDao.pwd().get());
		info.setvCode(mUserDao.vCode().get());
		info.setToken(mUserDao.token().get());
		info.setPicUrl(mUserDao.picUrl().get());
		City city = new City();
		city.setCity(mUserDao.city().get());
		city.setCityCode(mUserDao.cityCode().get());
		info.setCity(city);
		info.setCoupon(mUserDao.coupon().get());
		info.setIntegral(mUserDao.integral().get());
		info.setWallet(mUserDao.wallet().get());
		info.setSex(mUserDao.sex().get());
	}

	/**
	 * 设置本地缓存的所有用户信息
	 * 
	 * @tanglc
	 */
	public void cacheUserInfo(UserInfo userInfo) {
		setUserName(userInfo.getName());
		setUserNick(userInfo.getNick());
		setUserBirth(userInfo.getBirth());
		setUserPhone(userInfo.getPhone());
		setUservCode(userInfo.getvCode());
		setUserToken(userInfo.getToken());
		setUserPicUrl(userInfo.getPicUrl());
		setUserSex(userInfo.getSex());
		setUserCoupon(userInfo.getCoupon());
		setUserWallet(userInfo.getWallet());
		setUserIntegral(userInfo.getIntegral());
		saveAddrList(userInfo.getAddrList());
		setCity(userInfo.getCity());
	}

	/**
	 * 清空本地缓存的所有用户信息
	 * 
	 * @tanglc
	 */
	public void deleteUserDao() {
		mUserDao.clear();
		setUserPicUrl("");
	}

	/**
	 * 设置缓存中用户ID
	 * 
	 * @tanglc
	 */
	public void setUserId(String uid) {
		mUserDao.edit().id().put(uid).apply();
	}

	/**
	 * 设置缓存中用户姓名
	 * 
	 * @tanglc
	 */
	public void setUserName(String uName) {
		mUserDao.edit().name().put(uName).apply();
	}

	/**
	 * 设置缓存中用户昵称
	 * 
	 * @tanglc
	 */
	public void setUserNick(String nick) {
		mUserDao.edit().nick().put(nick).apply();
	}

	/**
	 * 设置缓存中用户生日
	 * 
	 * @tanglc
	 */
	public void setUserBirth(String birth) {
		mUserDao.edit().birth().put(birth).apply();
	}

	/**
	 * 设置缓存中用户手机
	 * 
	 * @tanglc
	 */
	public void setUserPhone(String phone) {
		mUserDao.edit().phone().put(phone).apply();
	}

	/**
	 * 设置缓存中用户密码
	 * 
	 * @tanglc
	 */
	public void setUserPwd(String pwd) {
		mUserDao.edit().pwd().put(pwd).apply();
	}

	/**
	 * 设置缓存中验证码
	 * 
	 * @tanglc
	 */
	public void setUservCode(String vCode) {
		mUserDao.edit().vCode().put(vCode).apply();
	}

	/**
	 * 设置缓存中token标志位
	 * 
	 * @tanglc
	 */
	public void setUserToken(String token) {
		mUserDao.edit().token().put(token).apply();
	}

	/**
	 * 设置缓存中用户头像地址
	 * 
	 * @tanglc
	 */
	public void setUserPicUrl(String picUrl) {
		mUserDao.edit().picUrl().put(picUrl).apply();
	}

	/**
	 * 设置缓存中用户性别
	 * 
	 * @tanglc
	 */
	public void setUserSex(int sex) {
		mUserDao.edit().sex().put(sex).apply();
	}

	/**
	 * 设置缓存中提示用户的更新次数
	 * 
	 * @tanglc
	 */
	public void setUpdateNum(int num) {
		mUserDao.edit().updateNum().put(num).apply();
	}

	/**
	 * 设置缓存中用户优惠券数量
	 * 
	 * @tanglc
	 */
	public void setUserCoupon(int coupon) {
		mUserDao.edit().coupon().put(coupon).apply();
	}

	/**
	 * 设置缓存中用户钱包余额
	 * 
	 * @tanglc
	 */
	public void setUserWallet(float wallet) {
		mUserDao.edit().wallet().put(wallet).apply();
	}

	/**
	 * 设置缓存中用户积分
	 * 
	 * @tanglc
	 */
	public void setUserIntegral(int integral) {
		mUserDao.edit().integral().put(integral).apply();
	}

	/**
	 * 设置缓存中用户所在城市
	 * 
	 * @tanglc
	 */
	public void setCity(City city) {
		mUserDao.edit().city().put(city.getCity()).apply();
		mUserDao.edit().cityCode().put(city.getCityCode()).apply();
	}

	/**
	 * 添加用户地址列表 tanglc
	 * 
	 * @param list
	 */
	public void saveAddrList(List<MyAddress> list) {
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				MyAddress myAddress = list.get(i);
				if (myAddress != null) {
					String contact = myAddress.getContact();
					String phone = myAddress.getPhone();
					if (TextUtils.isEmpty(contact)) {
						contact = getUserInfo().getNick();
						myAddress.setContact(contact);
					}
					if (TextUtils.isEmpty(phone)) {
						phone = getUserInfo().getPhone();
						myAddress.setPhone(phone);
					}
				}
			}

		}
		addressDao.setList(list);
	}

	/**
	 * 获取用户地址列表 tanglc
	 * 
	 * @param list
	 */
	public List<MyAddress> queryAddrList() {
		List<MyAddress> list = addressDao.getList();
		return list;
	}

	/**
	 * 获取地址搜索关键字集合
	 * 
	 * @param context
	 * @return
	 */
	public List<MyAddress> getAdreessKeys() {
		List<MyAddress> list = (List<MyAddress>) SharedpreferncesUtil.readObj(
				mContext, ADDRESS_KEYS);
		if (list == null) {
			list = new ArrayList<MyAddress>();
		}
		return list;
	}

	/**
	 * 清空地址搜索关键字集合
	 */
	public void clearAdreessKeys() {
		SharedpreferncesUtil.clearSearchAddr(mContext);
	}

	/**
	 * 用于修复2.0覆盖安装1.9版本，账号信息不能保留的bug
	 * 
	 * @return
	 */
	public User getOldUserInfo() {
		try {
			User user = (User) CompatiblePreferncesUtil.readObj(mContext, USER);
			return user;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;

	}

	/**
	 * 清空1.9的用户数据
	 */
	public void clearOldUserInfo() {
		CompatiblePreferncesUtil.clearByKey(USER, mContext);
	}

	/**
	 * java生成随机数字和字母组合
	 * 
	 * @param length
	 *            [生成随机数的长度]
	 * @return
	 */
	public String getCharAndNumr(int length) {
		String val = "";
		Random random = new Random();
		for (int i = 0; i < length; i++) {
			// 输出字母还是数字
			String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
			// 字符串
			if ("char".equalsIgnoreCase(charOrNum)) {
				// 取得大写字母还是小写字母
				int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
				val += (char) (choice + random.nextInt(26));
			} else if ("num".equalsIgnoreCase(charOrNum)) { // 数字
				val += String.valueOf(random.nextInt(10));
			}
		}
		return val;
	}

	/**
	 * 保存地址搜索关键字
	 * 
	 * @param context
	 * @param keys
	 */
	public void putAddressKeys(List<MyAddress> keys) {
		if (keys.size() > 10) {// 最多保存10个记录
			keys.remove(10);
		}
		SharedpreferncesUtil.saveObj(mContext, keys, ADDRESS_KEYS);
	}

	// queryUserInfo的假数据
	public Result<UserInfo> queryUserInfoTest() {
		Result<UserInfo> result = new Result<UserInfo>();
		UserInfo userInfo = setUserInfo();
		result.setCode(1);
		result.setMsg("登录成功");
		result.setData(userInfo);
		return result;
	}

	// 保存用户的信息
	public UserInfo setUserInfo() {
		UserInfo userInfo = new UserInfo();
		userInfo.setuId("001");
		userInfo.setName("赵云");
		userInfo.setNick("小云");
		userInfo.setPhone("13547859636");
		userInfo.setPwd("123456");
		userInfo.setBirth("1989-02-13");
		userInfo.setPicUrl("http://115.29.249.152:707/getPicture?Key=UserPic/140424_201503181721258071_MyFile.png");
		userInfo.setBigPicUrl("http://115.29.249.152:707/getPicture?Key=UserPic/140424_201503181721258071_MyFile.png");
		userInfo.setSex(1);
		userInfo.setCoupon(8);
		userInfo.setWallet(230);
		userInfo.setIntegral(5000);
		List<MyAddress> list = setMyAddress();
		userInfo.setAddrList(list);
		return userInfo;
	}

	/**
	 * 2.29 密码验证接口
	 * 
	 * @param uId
	 * @return UserInfo
	 */
	public CodeResult pwdCheck(PwdCheckReq pwdCheckReq) throws RetrofitError {
		MD5Req<PwdCheckReq> md5Req = new MD5Req<PwdCheckReq>(pwdCheckReq);
		CodeResult result = userService.pwdCheck(md5Req.jsonParams,
				md5Req.isEncrypted, md5Req.timeStamp, md5Req.randomNum,
				md5Req.sign);
		return result;
	}

	// updateUserInfo 的假数据
	public CodeResult mockUpdateUserInfo() {
		CodeResult result = new CodeResult();
		result.code = 1;
		result.msg = "修改成功";
		return result;
	}

	// ManagerAddr 的假数据
	public CodeListResult<MyAddress> mockManagerAddr() {
		CodeListResult<MyAddress> res = new CodeListResult<MyAddress>();
		List<MyAddress> list = new ArrayList<MyAddress>();
		MyAddress myAddress = null;
		for (int i = 0; i < 3; i++) {
			myAddress = new MyAddress();
			myAddress.setAddrId("00" + String.valueOf(i));
			myAddress.setAddrName("上海杨浦区淞沪路308号创智天地7号楼00" + String.valueOf(i));
			myAddress.setContact("关羽");
			myAddress.setSex(1);
			myAddress.setPhone("13456898956");
			myAddress.setCityCode("0012");
			myAddress.setCityName("上海");
			myAddress.setLat("30.235689");
			myAddress.setLng("120.235689");
			myAddress.setState(0);
			list.add(myAddress);
		}
		res.code = 1;
		res.msg = "修改成功";
		res.list = list;
		return res;
	}

	// MyShopList 的假数据
	int tempPage = 1;

	private CodeListResult<ShopItem> mockMyShopList() {
		CodeListResult<ShopItem> res = new CodeListResult<ShopItem>();
		res.code = 1;
		res.msg = "成功";
		res.page = ++tempPage;
		List<ShopItem> list = new ArrayList<ShopItem>();
		for (int i = 0; i < 5; i++) {
			ShopItem item = new ShopItem();
			item.setsId("SHD01909");
			item.setsName("天下第一家");
			item.setPicUrl("http://a.hiphotos.baidu.com/image/pic/item/d833c895d143ad4bc15daafa81025aafa40f069c.jpg");
			item.setAddress("杨浦区淞沪路316号");
			item.setAvgReceive("30");
			item.setAvgSend("20");
			item.setDistance("300");
			item.setLat("31.30687800");
			item.setLng("121.51326000");
			item.setNotice("满50减50");
			item.setPhone("110");
			item.setSalesQty("200");
			item.setScore(4.50f);
			item.setCommentsNum("30");
			item.setSpending(20.10f);
			item.setState(1);
			list.add(item);
		}
		for (int i = 0; i < 7; i++) {
			ShopItem item = new ShopItem();
			item.setsId("SHP20151025");
			item.setsName("龙涛大排档");
			item.setPicUrl("http://c.hiphotos.baidu.com/image/pic/item/c83d70cf3bc79f3dca9c5f41bba1cd11728b2945.jpg");
			item.setAddress("杨浦区淞沪路388号政通路");
			item.setAvgReceive("45");
			item.setAvgSend("36");
			item.setDistance("800");
			item.setLat("31.30687800");
			item.setLng("121.51326000");
			item.setNotice("满100减20");
			item.setPhone("13115689562");
			item.setSalesQty("320");
			item.setScore(3.50f);
			item.setCommentsNum("88");
			item.setSpending(12.50f);
			item.setState(2);
			list.add(item);
		}
		for (int i = 0; i < 5; i++) {
			ShopItem item = new ShopItem();
			item.setsId("SH01909");
			item.setsName("小星星水果店");
			item.setPicUrl("http://daili.ideliver.cn/upload/635497558213693037_SH0190982846462.JPG");
			item.setAddress("创智天地广场7号楼602室");
			item.setAvgReceive("55");
			item.setAvgSend("38");
			item.setDistance("1120");
			item.setLat("31.30687800");
			item.setLng("121.51326000");
			item.setNotice("满1000减300,满100减30");
			item.setPhone("15869563195");
			item.setSalesQty("560");
			item.setScore(4.50f);
			item.setCommentsNum("122");
			item.setSpending(26.80f);
			item.setState(3);
			list.add(item);
		}
		res.list = list;
		return res;
	}

	// queryMyOrder 的假数据
	private Result<List<MyOrderItem>> mockQueryMyOrder() {
		Result<List<MyOrderItem>> result = new Result<List<MyOrderItem>>();
		result.code = 1;
		result.msg = "成功";
		List<MyOrderItem> list = new ArrayList<MyOrderItem>();
		MyOrderItem order1 = new MyOrderItem();
		order1.setoId("SH01879DC1501280014");
		order1.setoTime("今天11:30");
		order1.setsName("曹林米线");
		order1.setNum(7);
		order1.setoPrice(22);
		order1.oDate = "今天";
		order1.setPhone("186167545241");
		order1.setState(2);
		order1.setSummary("可乐1份，米粉一碗，鸡翅一个...");
		order1.setAcceptTime(1441888543 - 5000);
		order1.setoTimeStamp(1441888543 - 60000);
		order1.setRemainTime(50);
		list.add(order1);
		for (int i = 0; i < 20; i++) {
			MyOrderItem order = new MyOrderItem();
			order.setoId("SH01879DC1501280014");
			order.setoTime("今天11:30");
			order.setsName("曹林米线");
			order.setNum(7);
			order.setoPrice(22);
			order.oDate = "今天";
			order.setPhone("186167545241");
			order.setState(new Random().nextInt(17));
			order.setSummary("可乐1份，米粉一碗，鸡翅一个...");
			if( i<5 ){
				order.setAcceptTime(1441936381);
				order.setoTimeStamp(1441936381);
			}else if( i<10 ){
				order.setAcceptTime(1441936381 - 5000);
				order.setoTimeStamp(1441936381 - 60000);
			}else if ( i< 15 ){
				order.setAcceptTime(1441936381 - 5000 - 259200);
				order.setoTimeStamp(1441936381 - 60000 - 259200);
			}else if(i<20){
				order.setAcceptTime(1441936381 - 5000 - 259200 - 86400);
				order.setoTimeStamp(1441936381 - 60000 - 259200 - 86400);
			}
			
			list.add(order);
		}
		result.setData(list);
		return result;

	}

	// queryMyOrder 的假数据
	private CodeResult<CheckUpdateRes> mockCheckUpdateRes() {
		CodeResult<CheckUpdateRes> res = new CodeResult<CheckUpdateRes>();
		res.code = 1;
		res.msg = "成功";
		CheckUpdateRes checkUpdateRes = new CheckUpdateRes();
		checkUpdateRes.setIsUpdate(1);
		checkUpdateRes.setDownloadUrl("");
		checkUpdateRes
				.setUpdateInfo("【新增】积分\r\n【新增】在线支付\r\n【优化】丰富首页内容\r\n【优化】订单结算界面\r\n【新增】积分\r\n【新增】在线支付\r\n【优化】丰富首页内容\r\n【优化】订单结算界面【新增】积分\r\n【新增】在线支付\r\n【优化】丰富首页内容\r\n【优化】订单结算界面【新增】积分\r\n【新增】在线支付\r\n【优化】丰富首页内容\r\n【优化】订单结算界面");
		res.bean = checkUpdateRes;
		return res;
	}

	public List<MyAddress> setMyAddress() {
		List<MyAddress> list = new ArrayList<MyAddress>();
		MyAddress myAddress = null;
		for (int i = 0; i < 3; i++) {
			myAddress = new MyAddress();
			myAddress.setAddrId("00" + String.valueOf(i));
			myAddress.setAddrName("上海杨浦区淞沪路308号创智天地7号楼");
			myAddress.setContact("关羽");
			if (i == 0) {
				myAddress.setSex(2);
			} else {
				myAddress.setSex(1);
			}
			myAddress.setPhone("13456898956");
			myAddress.setCityCode("0012");
			myAddress.setCityName("上海");
			myAddress.setLat("30.235689");
			myAddress.setLng("120.235689");
			list.add(myAddress);
		}
		return list;
	}

}
